/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package issr.boundary;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import issr.boundary.vaadin.AbstractView;
import issr.boundary.vaadin.MainApplication;
import issr.controller.ControlloreLogin;
import issr.entity.Utente;
import issr.misc.Labels;

/**
 *
 * @author Soulreaver
 */
public class PannelloLogin extends AbstractView implements Button.ClickListener {

    private ControlloreLogin controllore;
    private final TextField userField = new TextField(Labels.LOGIN);
    private final PasswordField passwordField = new PasswordField(Labels.PASSWORD);
    private final FormLayout form;
    private final Button submit;
    // Allow input with the enter button.

    public PannelloLogin() {
        controllore = new ControlloreLogin();
        form = new FormLayout();
        setCompositionRoot(form);
        form.setMargin(true);
        form.setHeight("100%");
        form.setStyleName("view");

        userField.setValue("admin");
        form.addComponent(userField);
        passwordField.setValue("123");
        form.addComponent(passwordField);
        submit = new Button("Submit", this);
        submit.setClickShortcut(KeyCode.ENTER);
        form.addComponent(submit);

    }

    @Override
    protected void close() throws Exception {
    }

    @Override
    protected void open() throws Exception {
    }

    @Override
    public void buttonClick(ClickEvent event) {
        Utente utente = controllore.loginUtente((String) userField.getValue(), (String) passwordField.getValue());
        if (utente != null) {
            MainApplication.getProject().getMainWindow().showNotification(
                    "Login effetuato con successo");
            MainApplication.getProject().setUser((Object) utente);
        } else {
            MainApplication.getProject().getMainWindow().showNotification("Credenziali errate");
        }
    }
}
